#include "cell_main.h"
#include "ui_cell_main.h"
#include<QDir>
#include<QtDebug>
#include<QDateTime>
#include<QFileDialog>
#include<QMessageBox>
#include<QDesktopServices>

Cell_main::Cell_main(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Cell_main)
{
    ui->setupUi(this);
    //获取当前应用程序所在目录
    m_strDataPath= QApplication::applicationDirPath()+"/data/";
    QDir d(m_strDataPath);
    if(!d.exists())
    {
        d.mkdir(m_strDataPath);
    }

    m_model=new QStandardItemModel;
    m_timer.start(500);
    connect(&m_timer,&QTimer::timeout,this,&Cell_main::updateFile);

    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);// 不可编辑
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);//选中整行

}

Cell_main::~Cell_main()
{
    delete ui;
    delete m_model;
}

void Cell_main::updateFile()
{

    QDir d(m_strDataPath);//获取路径
    QStringList lFilter;
    lFilter<<"*.txt";
    QFileInfoList lFilesInfo= d.entryInfoList(lFilter,QDir::Files);//遍历文件夹

    QString strFilter=ui->lineEdit->text();//过滤参数
    QString strFlag;//临时flag
    QList<QStandardItem*>lRow;//每一行的数据列表

    QStandardItemModel *temModel=new QStandardItemModel;


    for(int i=0;i<lFilesInfo.size();i++)//遍历所有文件
    {
        lRow.clear();
        QFileInfo &info=lFilesInfo[i];
        if(!info.completeBaseName().contains(strFilter))
        {
            continue;
        }//过滤器判断、筛选

        //序号
        QStandardItem *p1=new QStandardItem(info.completeSuffix());
        strFlag=strFlag+info.absoluteFilePath();
        QStandardItem *p2=new QStandardItem(info.absoluteFilePath());
        QStandardItem *p3=new QStandardItem(info.baseName());
        QStandardItem *p4=new QStandardItem(info.birthTime().toString("yyyy/MM/dd hh:mm:ss"));
        QStandardItem *p5=new QStandardItem(info.lastModified().toString("yyyy/MM/dd hh:mm:ss"));
        QStandardItem *p6=new QStandardItem(QString::number(info.size()/1024)+"KB");
       lRow<<p1<<p2<<p3<<p4<<p5<<p6;
       temModel->appendRow(lRow);//放到数据模型里

    }

    if(strFlag==m_Cur)
    {
        delete temModel;
        return;
    }//如果当前缓存文件和目标文件一样，没有必要去放

    //否则更新模型数据，放到控件里
    delete m_model;
    m_model=nullptr;
    m_model=temModel;
    m_Cur=strFlag;
    QStringList lHeaders{"文件类型","绝对路径","文件名","创建日期","最后修改日期","文件大小"};
    m_model->setHorizontalHeaderLabels(lHeaders);
    ui->tableView->setModel(m_model);
}

void Cell_main::on_btn_upload_clicked()
{
    auto str= QFileDialog::getOpenFileName(nullptr,"upload",QDir::homePath(),"*.txt");
    if(str.isEmpty())
    {
        return;
    }
    auto uploadPath=m_strDataPath+str.section("/",-1);
    //判断缓存目录中是否有相同文件名的文件
    if(QFile::exists(uploadPath))
    {
        QMessageBox::warning(nullptr,"文件已存在","请重新改名！");
        return;
    }
    bool ret= QFile::copy(str,uploadPath);
    QMessageBox::warning(nullptr,"信息",ret?"成功":"上传失败，可能被占用");
}

void Cell_main::on_btn_delete_clicked()
{
    auto index= ui->tableView->currentIndex();//改行被选中的数据的索引
    if(!index.isValid())
    {
        return;
    }//无效则返回
    auto itemp= m_model->index(index.row(),1);//改行第一列数据的索引
    auto str= m_model->data(itemp).toString();//通过索引拿到数据
    auto ret=QFile::remove(str);
    QMessageBox::information(nullptr,"信息",ret?"文件删除成功":"文件删除失败");
}

void Cell_main::on_btn_open_clicked()
{
QDesktopServices::openUrl(QUrl(m_strDataPath));
}


